var guiCode = require('../../utils/guiCode.js');
const app = getApp()

Page({
    data: {
        registerUser: {
            username: '',
            password: '',
            role: '',
            gender: '男',
            avatar: '/static/avatar/avatar_male.jpg',
        },
        switch_icon: 'switch_female-copy',
        confirmPassword: '',
        guiCode: '',
        isUsernameVisable: false,
        isPasswordVisable: false,
        isConfirmPasswordVisable: false,
    },
    onShow: function (options) {
        this.guiCode = new guiCode({
            el: '#guiCodeCanvas',
            width: 70,
            height: 50,
            createCodeImg: ""
        });
        console.log("code==>" + this.guiCode);
    },

    /**
     * 刷新图形验证码
     */
    guiCodeTap() {
        this.guiCode.refresh();
    },

    inputUsername: function (e) {
        this.setData({
            'registerUser.username': e.detail.value
        });
    },
    inputPassword: function (e) {
        this.setData({
            'registerUser.password': e.detail.value
        });
    },

    inputConfirmPassword: function (e) {
        this.setData({
            'confirmPassword': e.detail.value
        });
    },

    // 选择角色
    selectRole(e) {
        // console.log("role==>" + e.detail.value);
        this.setData({
            'registerUser.role': e.detail.value
        });
    },

    switchAvatar: function () {
        if (this.data.registerUser.avatar == '/static/avatar/avatar_male.jpg') {
            this.setData({
                'registerUser.avatar': '/static/avatar/avatar_female.jpg',
                'registerUser.gender': '女',
                'switch_icon': 'switch_male-copy',
            });
        } else {
            this.setData({
                'registerUser.avatar': '/static/avatar/avatar_male.jpg',
                'registerUser.gender': '男',
                'switch_icon': 'switch_female-copy',
            });
        }
    },

    /**
     * 验证用户名，密码，角色，图形验证码
     */
    toRegister: function () {
        console.log("username==>", this.data.registerUser.username);
        if (!this.data.registerUser.username) {
            this.setData({
                isUsernameVisable: true
            });
            return;
        }
        if (!this.data.registerUser.password) {
            this.setData({
                isPasswordVisable: true,
                isUsernameVisable: false
            });
            return;
        }
        if (!this.data.confirmPassword) {
            this.setData({
                isPasswordVisable: false,
                isUsernameVisable: false,
                isConfirmPasswordVisable: true,
            });
            return;
        }
        this.setData({
            isPasswordVisable: false,
            isUsernameVisable: false,
            isConfirmPasswordVisable: false
        });
        if (this.data.registerUser.password.length < 6) {
            wx.showToast({
                title: '为了账号安全，密码长度要大于6',
                icon: 'none'
            });
            return;
        }
        if (this.data.registerUser.password != this.data.confirmPassword) {
            wx.showToast({
                title: '两次密码输入不一致',
                icon: 'none'
            });
            return;
        }
        if (!this.data.registerUser.role) {
            wx.showToast({
                title: '请选择角色',
                icon: 'none'
            });
            return
        }
        if (!this.data.guiCode) {
            wx.showToast({
                title: '请输入图形验证码',
                icon: 'none'
            })
            return
        }
        let res = this.guiCode.validate(this.data.guiCode)
        if (!res) {
            wx.showToast({
                title: '图形验证码错误',
                icon: 'error'
            })
        }
        let registerUser = this.data.registerUser;
        //开始登录
        wx.request({
            url: 'http://localhost:8282/public/register',
            method: 'POST',
            data: registerUser,
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: (res) => {
                console.log("res==>", res.data);
                if (res.data.code == 200) {
                    wx.setStorageSync('loginUser', res.data.data);
                    wx.setStorageSync('isLogin', true);
                    wx.switchTab({
                        url: '/pages/index/index'
                    });
                } else {
                    wx.showToast({
                        title: res.data.desc,
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                console.log(err);
                wx.showToast({
                    title: '登录接口调用失败',
                    icon: 'error'
                })
            }
        })
    },

    /**
     * 立即验证按钮监听
     */
    toValidate() {
        //验证图形验证码
        this.validateGuiCode()
    },

})